﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using Intelligent.OTC.Common.UnitOfWork;
using Intelligent.OTC.Business;
using Intelligent.OTC.Common;
using System.Configuration;
using System.IO;
using Intelligent.OTC.Common.Utils;
using Intelligent.OTC.WebApi.Core;
using Intelligent.OTC.Common.Exceptions;
using Intelligent.OTC.Domain.DataModel;
using System.Net.Http;
using System.Net;
using Intelligent.OTC.Common.Attr;
using System.Web.OData.Query;
using System.Web.OData.Extensions;
using Intelligent.OTC.Domain.Dtos;


namespace Intelligent.OTC.WebApi.Controllers
{
    [UserAuthorizeFilter(actionSet: "soa")]
    public class CollectorSoaController : ApiController
    {
        [HttpGet]
        [PagingQueryable]
        public IEnumerable<SoaDto> Get(string invoiceState , string invoiceTrackState , string legalEntity , string invoiceNum, string soNum, string poNum, string invoiceMemo)
        {
            ISoaService service = SpringFactory.GetObjectImpl<ISoaService>("SoaService");

            //ODataQuerySettings setting = new ODataQuerySettings();
            return service.GetSoaList(invoiceState, invoiceTrackState, legalEntity, invoiceNum, soNum, poNum, invoiceMemo);
        }
        #region bak
        //public HttpResponseMessage Get(ODataQueryOptions<CollectorSoaView> queryOptions)
        //{
        //    ISoaService service = SpringFactory.GetObjectImpl<ISoaService>("SoaService");

        //    ODataQuerySettings setting = new ODataQuerySettings();
        //    IQueryable res = queryOptions.Filter.ApplyTo(service.GetSoaList().AsQueryable(), setting);

        //    //dynamic sumRes = (from c in res.Cast<CollectorSoaView>()
        //    //                  group c by 1 into g
        //    //                  select new
        //    //                  {
        //    //                      T1 = g.Sum(soa => soa.TotalAmt),
        //    //                      T2 = g.Sum(soa => soa.PastDueAmount),
        //    //                      T3 = g.Sum(soa => soa.FDueOver90Amt),
        //    //                      T4 = g.Sum(soa => soa.CreditLimit)
        //    //                  }).FirstOrDefault();

        //    long? count = 0;
        //    if (queryOptions.Count != null)
        //    {
        //        count = queryOptions.Count.GetEntityCount(res);
        //    }
        //    if (queryOptions.OrderBy != null)
        //    {
        //        res = queryOptions.OrderBy.ApplyTo(res, setting);
        //    }

        //    if (queryOptions.Skip != null)
        //    {
        //        res = queryOptions.Skip.ApplyTo(res, setting);
        //    }

        //    if (queryOptions.Top != null)
        //    {
        //        res = queryOptions.Top.ApplyTo(res, setting);
        //    }

        //    if (queryOptions.Count != null)
        //    {
        //        return Request.CreateResponse<AlertExtention<CollectorSoaView>[]>(
        //            HttpStatusCode.OK,
        //            new AlertExtention<CollectorSoaView>[] { new AlertExtention<CollectorSoaView>(res.Cast<CollectorSoaView>(), count){
        //                 //TotalAmount = sumRes==null?0:sumRes.T1, 
        //                 //TotalPastDue = sumRes==null?0:sumRes.T2,
        //                 //TotalOver90Days = sumRes==null?0:sumRes.T3,
        //                 //TotalCreditLimit = sumRes==null?0:sumRes.T4
        //            } });

        //    }
        //    else
        //    {
        //        return Request.CreateResponse(HttpStatusCode.OK, res);
        //    }
        //}
        #endregion

        [HttpGet]
        public IEnumerable<SoaDto> GetNoPaging(string ListType)
        {
            ISoaService service = SpringFactory.GetObjectImpl<ISoaService>("SoaService");
            return service.GetNoPaging(ListType).AsQueryable();
        }

        //execute soa (Send Soa Page)
        /// <summary>
        /// execute soa (Send Soa Page)
        /// </summary>
        /// <param name="ColSoa">custNums</param>
        /// <param name="Type">create/view soa Detail  create:start WF;view: not start WF</param>
        /// <returns></returns>
        [HttpGet]
        public IEnumerable<SendSoaHead> CreateSoa(string ColSoa,string Type)
        {
            ISoaService service = SpringFactory.GetObjectImpl<ISoaService>("SoaService");
            return service.CreateSoa(ColSoa,Type).AsQueryable();
        }

        //open a soa in work in process(collectorSoa Page)
        [HttpGet]
        public CollectorAlert GetSoa(string TaskNo) 
        {
            ISoaService service = SpringFactory.GetObjectImpl<ISoaService>("SoaService");
            return service.GetSoa(TaskNo);
        }

        //get status of a soa
        [HttpGet]
        public CollectorAlert GetStatus(string ReferenceNo)
        {
            ISoaService service = SpringFactory.GetObjectImpl<ISoaService>("SoaService");
            return service.GetStatus(ReferenceNo);
        }

        //view invoice log in sendsoa
        [HttpGet]
        public IEnumerable<InvoiceLog> GetInvLog(string InvNum) 
        {
            ISoaService service = SpringFactory.GetObjectImpl<ISoaService>("SoaService");
            return service.GetInvLog(InvNum).AsQueryable();
        }

        [HttpGet]
        public IEnumerable<Contactor> GetSoaContact(string CustNumFCon) 
        {
         //   ISoaService service = SpringFactory.GetObjectImpl<ISoaService>("SoaService");
         //   return service.GetSoaContact(CustNumFCon);
            ContactService service = SpringFactory.GetObjectImpl<ContactService>("ContactService");
            return service.GetContactByCustomer(CustNumFCon);
        }

        [HttpGet]
        public IEnumerable<CustomerPaymentBank> GetSoaPayment(string CustNumFPb)
        {
            ISoaService service = SpringFactory.GetObjectImpl<ISoaService>("SoaService");
            return service.GetSoaPayment(CustNumFPb);
        }

        [HttpGet]
        public IEnumerable<CustomerPaymentCircle> GetSoaPaymentCircle(string CustNumFPc)
        {
            ISoaService service = SpringFactory.GetObjectImpl<ISoaService>("SoaService");
            return service.GetSoaPaymentCircle(CustNumFPc);
        }

        [HttpGet]
        public IEnumerable<ContactorDomain> GetSoaContactDomain(string CustNumFPd)
        {
            ISoaService service = SpringFactory.GetObjectImpl<ISoaService>("SoaService");
            return service.GetSoaContactDomain(CustNumFPd);
        }

        [HttpGet]
        public IEnumerable<ContactHistory> GetContactHistory(string CustNumsFCH)
        {
            ISoaService service = SpringFactory.GetObjectImpl<ISoaService>("SoaService");
            return service.GetContactHistory(CustNumsFCH);
        }

        [HttpGet]
        public IEnumerable<PeriodControl> GetAllPeriod(string Period)
        {
            ISoaService service = SpringFactory.GetObjectImpl<ISoaService>("SoaService");
            return service.GetAllPeriod();
        }

        [HttpGet]
        public IEnumerable<SoaDto> SelectChangePeriod(int PeriodId)
        {
            ISoaService service = SpringFactory.GetObjectImpl<ISoaService>("SoaService");
            return service.SelectChangePeriod(PeriodId);
        }

        [HttpPost]
        public void Wfchange(string referenceNo, string type) {
            ISoaService service = SpringFactory.GetObjectImpl<ISoaService>("SoaService");
            service.Wfchange("4", referenceNo, type);
            
        }

        //batch sendsoa 
        [HttpPost]
        public void BatchSoa(string Cusnums)
        {
            ISoaService service = SpringFactory.GetObjectImpl<ISoaService>("SoaService");
            service.BatchSoa(Cusnums);
        }

        //save invoice comment
        [HttpPost]
        public void SaveComm(int Invid, string Comm)
        {
            if (Invid != 0)
            {
                ISoaService service = SpringFactory.GetObjectImpl<ISoaService>("SoaService");
                service.SaveComm(Invid, Comm);
            }
        }

        [HttpPost]
        //save Notes
        public void SaveNotes(string Cus, string SpNotes)
        {
            ISoaService service = SpringFactory.GetObjectImpl<ISoaService>("SoaService");
            service.SaveNotes(Cus, SpNotes);
        }

        //common Post
        [HttpPost]
        public void CommonSave([FromBody]List<string> list)
        {
            if (list[0] == "1") 
            {
                SpecialNotesService service = SpringFactory.GetObjectImpl<SpecialNotesService>("SpecialNotesService");
                service.AddOrUpdateByPara(list[1], list[2], list[3]);
            }
            else if (list[0] == "2") 
            {
                ISoaService service = SpringFactory.GetObjectImpl<ISoaService>("SoaService");
                service.SaveComm(Convert.ToInt32(list[1]), list[2]);
            }
            else if (list[0] == "3") 
            {
                ContactService service = SpringFactory.GetObjectImpl<ContactService>("ContactService");
                service.insertInvoiceLogForNotice(list);
            }
            else if (list[0] == "4")
            {
                ContactService service = SpringFactory.GetObjectImpl<ContactService>("ContactService");
                service.insertInvoiceLogForPtp(list);
            }
            else if (list[0] == "5") {
                ISoaService service = SpringFactory.GetObjectImpl<ISoaService>("SoaService");
                service.BatchSaveComm(list[1], list[2]);
            }
        }


        [HttpGet]
        public int CheckPermission(string ColSoa)
        {
            ISoaService service = SpringFactory.GetObjectImpl<ISoaService>("SoaService");
            return service.CheckPermission(ColSoa);
        }

    }
}